﻿using Panuon.UI.Silver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfBill.Data;

namespace WpfBill.GUI
{
    /// <summary>
    /// WinBill.xaml 的交互逻辑
    /// </summary>
    public partial class WinBill : WindowX
    {
        public WinBill()
        {
            InitializeComponent();
            Init();
        }
        IList<Project> projects = null;
        /// <summary>
        /// 工程信息
        /// </summary>
        private Project project = null;

        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            try
            {
                projects = Project.FindAll();
                // cmdProjectName.ItemsSource = projects.Select(p => p.Name).ToList();
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="proInfo"></param>
        public bool? ShowDialog(Project proInfo)
        {
            project = proInfo;
            txtProName.Text = project.Name;
            txtBalance.Text = "￥" + project.Balance.ToString("0.00");
            cmbComNames.Items.Add(project.EnterpriseA);
            cmbComNames.Items.Add(project.EnterpriseB);
            cmbComNames.SelectedIndex = 0;
            return this.ShowDialog();
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Business business = new Business() { ProjectID = project.ID, };
                business.Amount = decimal.Parse(txtAmount.Text.Trim());
                business.BusinessDate = dtPicker.SelectedDateTime;
                business.EnterpriseName = cmbComNames.Text.ToString();
                business.Account = txtAccount.Text.ToString();
                business.Person = txtPerson.Text.ToString();
                business.LoanType = cmbType.SelectedIndex == 0 ? 1 : 2;
                decimal balance = business.LoanType == 1 ? project.Balance + business.Amount : project.Balance - business.Amount;
               // business.Balance = balance;
                business.Insert();
                project.Balance = balance;
                project.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void CmbComNames_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbComNames.SelectedIndex == 0)
            {
                txtAccount.Text = project.AccountA;
                txtPerson.Text = project.PersonA;
            }
            else if (cmbComNames.SelectedIndex == 1)
            {
                txtAccount.Text = project.AccountB;
                txtPerson.Text = project.PersonB;
            }
        }

        private void CmbComNames_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

        }
    }
}
